package com.jzcn.admin.controller.system;

import com.jzcn.admin.annotation.ServerLog;
import com.jzcn.admin.base.BaseController;
import com.jzcn.common.dto.system.resources.ResourcesDTO;
import com.jzcn.common.dto.system.user.SearchUserDTO;
import com.jzcn.common.dto.system.user.SysUserDTO;
import com.jzcn.common.dto.system.user.UserListDTO;
import com.jzcn.common.dto.system.user.UserPasswordDTO;
import com.jzcn.common.response.ResponseResult;
import com.jzcn.common.utils.JzPage;
import com.jzcn.domain.SysUser;
import com.jzcn.service.SysResourcesService;
import com.jzcn.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(value = "User", tags = "用户管理")
@RequestMapping("/api/user")
public class UserController extends BaseController {

    @Resource
    private SysUserService sysUserService;

    @Resource
    SysResourcesService sysResourcesService;

    @ApiOperation("获取用户信息")
    @GetMapping("/info/{id}")
    @RequiresPermissions("system:user:info")
    public ResponseResult<SysUser> getById(@PathVariable("id") @ApiParam("获取Id") Integer id) {
        return ResponseResult.success(sysUserService.getInfo(id));
    }

    @ApiOperation("当前用户登录菜单")
    @GetMapping("/tree")
    public ResponseResult<List<ResourcesDTO>> allMenu(){
        return ResponseResult.success(sysResourcesService.currentUserTree(getUserId()));
    }

    @ApiOperation("用户分页列表")
    @GetMapping("/page")
    @RequiresPermissions("system:user:page")
    @ServerLog(value ="用户分页列表", code= "system:user:page")
    public ResponseResult<JzPage<UserListDTO>> list(SearchUserDTO searchUser) {
        return ResponseResult.success(sysUserService.listPage(searchUser));
    }

    @ApiOperation("新增用户")
    @PostMapping("/create")
    @RequiresPermissions("system:user:create")
    @ServerLog(value ="新增用户", code= "system:user:create")
    public ResponseResult<Boolean> create(@RequestBody SysUserDTO sysUserDTO) {
        return ResponseResult.success(sysUserService.create(sysUserDTO));
    }

    @ApiOperation("更新用户")
    @PutMapping("/update/{id}")
    @RequiresPermissions("system:user:update")
    @ServerLog(value ="更新用户", code= "system:user:update")
    public ResponseResult<Boolean> update(@PathVariable("id") Integer id, @RequestBody SysUserDTO sysUserDTO) {
        return ResponseResult.success(sysUserService.update(sysUserDTO, id));
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/delete/{id}")
    @RequiresPermissions("system:user:delete")
    @ServerLog(value ="删除用户", code= "system:user:delete")
    public ResponseResult<Boolean> delete(@PathVariable("id") Integer id) {
        return ResponseResult.success(sysUserService.delete(id));
    }
    @ApiOperation("启用/禁用")
    @PostMapping("/status")
    @RequiresPermissions("system:user:status")
    public ResponseResult<Boolean> userStatus(@RequestParam("id") Integer id, @RequestParam Integer status){
        return ResponseResult.success(sysUserService.userStatus(id,status));
    }

    @ApiOperation("用户登录用户信息")
    @GetMapping("/current")
    public ResponseResult<Boolean> currEntEmployee() {
        return ResponseResult.success(sysUserService.getInfo(getUserId()));
    }

    @ApiOperation("修改用户密码")
    @PutMapping("/password/{id}")
    @ServerLog(value ="修改用户密码", code= "system:user:password")
    public ResponseResult<Boolean> pwd(@PathVariable("id") Integer id,
                                       @RequestBody UserPasswordDTO query){
        return ResponseResult.success(sysUserService.newPassword(id,query.getOldPassword(),query.getNewPassword()));
    }

    @PutMapping("/resetPwd/{id}")
    @ServerLog(value = "重置密码")
    public ResponseResult<Boolean> resetPwd(@RequestBody @PathVariable("id") Integer id) {
        return ResponseResult.success(sysUserService.resetPwd(id));
    }

}
